<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cool Cloud</title>

    <!-- Google Font: Source Sans Pro -->
    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
    <!-- DataTables -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.24/datatables.min.css"/>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/buttons/1.4.2/css/buttons.dataTables.min.css"/>
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/adminlte.min.css">
    <!--xcConfirm-->
    <link rel="stylesheet" href="dist/js/xcConfirm/css/xcConfirm.css">
    <!-- bootstrap选择-->
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/css/bootstrap-select.min.css">
    <link href="dist/css/foundation-datepicker.css" rel="stylesheet" type="text/css">
    <style>
        .tipmsg {
            background-color: #f39c12;
            position: absolute;
            z-index: 100;
            top: 30px;
            right: 3%;
            padding: 5px 5px 5px 5px;
            border-radius: 15px;
            border: 2px solid #ddd;
            box-shadow: 0 15px 15px -5px rgba(0, 0, 0, 0.2);
            color: white;
        }
        .modal-dialog {
            top: 40%;
        }
    </style>
</head>
<body class="hold-transition sidebar-mini">
<!-- wrapper -->
<div class="wrapper">
    <!-- Navbar -->
    <div th:replace="common/header :: header"></div>

    <!-- Main Sidebar Container -->
    <div th:replace="common/menu :: sidebar"></div>

    <!-- Content Wrapper. Contains page content -->
    <div th:replace="content/bgworkContent :: bgworkcontent"></div>

    <!-- /Footer -->
    <div th:replace="common/footer :: footer"></div>

    <!-- Control Sidebar -->
    <aside class="control-sidebar control-sidebar-dark"></aside>
</div>


<!--编辑个人信息-->
<div th:replace="common/editInforModel :: editinfo"></div>
<!--新增按钮-->
<button type="button" class="create" data-toggle="modal" data-target="#createWork" style="display: none"></button>
<div id="createWork" class="modal fade" style="margin-top: -10%" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">新增/编辑任务</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="form">
                    <!-- Single button -->
                    <input type="text" class="form-control" id="urlpicker" placeholder="下载链接...">
                    <h1></h1>

                    <input type="text" class="form-control" value="" id="datetimepicker" placeholder="执行时间...">

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" onclick="creatWork()" class="btn btn-primary">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
<!-- DataTables -->
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.24/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script>
<!--xcConfirm-->
<script src="dist/js/xcConfirm/js/xcConfirm.js"></script>
<!-- Page specific script -->
<!--bootstrap选择-->
<script src="plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/js/bootstrap-select.min.js"></script>
<script src="dist/js/foundation-datepicker.js"></script>
<script src="dist/js/foundation-datepicker.zh-CN.js"></script>
<!--邀请注册-->
<script src="dist/js/invite.js"></script>
<!--搜索-->
<script src="dist/js/search.js"></script>

<script>

    function editMyInfo() {
        $(".userName").val("[[${session.loginUser.userName}]]");
        $(".edit").click();
    }

    function submit() {
        let userName = $(".userName").val();
        let IwtNewPsd = $(".IwtNewPsd").val();
        let IwtOralPsd = $(".IwtOralPsd").val();
        let id = [[${session.loginUser.userId}]];
        if (userName == '') {
            alert("用户名不能为空！");
            return;
        }
        if ((IwtNewPsd == '' && IwtOralPsd != '') || (IwtNewPsd != '' && IwtOralPsd == '')) {
            alert("需要修改密码,原密码与新密码均不能为空！");
            return;
        }
        $.ajax({
            url: "UpdateInfo",
            type: "POST",
            data: {
                "id": id,
                "username": userName,
                "IwtNewPsd": IwtNewPsd,
                "IwtOralPsd": IwtOralPsd
            },
            success: function (date) {
                if (date.status == "psdwrong") {
                    alert("原密码错误！")
                } else if (date.status == "success" && IwtNewPsd != '') {
                    $("#logout").click();
                } else if (date.status == "success" && IwtNewPsd == '') {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  用户名修改成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    $('.close').click();
                    setInterval("location.reload( )", 1000);

                }
            }
        })
    }

    $('#datetimepicker').fdatepicker({
        format: 'yyyy-mm-dd hh:ii:ss',
        pickTime: true
    });


    function creatWork() {
        let time = $('#datetimepicker').val();
        var strtime = time.replace("/-/g", "/");//时间转换
        var date1 = new Date(strtime);
        var date2 = new Date();
        if (date1 < date2) {
            alert("选中时间必须在当前系统时间之后！")
            return;
        }
        let url = $('#urlpicker').val();
        $.ajax({
            url: "createBGWork",  //?sheduletime="+time+"&url="+url+"&sec="+Math.floor((date1-date2)/60000)
            type: 'POST',
            data: {
                sheduletime: time,
                url: url,
                sec: Math.floor((date1 - date2) / 60000)
            },
            success: function (result) {
                if (result.status == "success") {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  任务添加成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    $('.close').click();
                    reload();
                } else {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  任务添加失败！请联系管理员</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    $('.close').click();
                }
                closeTip5sec();
            }
        });
    }

    let table = null;
    $(function () {

        $.fn.dataTable.ext.buttons.batchDel = {
            action: function (e, dt, node, config) {
                //获取勾选中的id
                let ids = new Array;
                let allcount = 0;
                $("input[name='cb_select']:checked").each(function () {
                    console.log($(this).val().split(",")[0], $(this).val().split(",")[1])
                    allcount++;
                    if ($(this).val().split(",")[1] != 0) {
                        ids.push($(this).val().split(",")[0]);
                    }
                });
                ids = ids.join(",");
                if (ids == "" && allcount != 0) {
                    $(".tipmsg").html($('<span  style="margin-right: 25px"> 所选任务均已部署，无法删除！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    closeTip5sec();
                    return;
                } else if (ids == "" && allcount == 0) {
                    $(".tipmsg").html($('<span  style="margin-right: 25px"> 未勾选数据哦！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    closeTip5sec();
                    return;
                }
                $.ajax({
                    url: "batchDelBGWork",
                    type: 'POST',
                    data: {pks: ids},
                    success: function (result) {
                        if (result.status == "success" && allcount != ids.length) {
                            $(".tipmsg").html($('<span  style="margin-right: 25px">  部分删除成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                            $(".tipmsg").show();
                            reload();
                        } else if (result.status == "success" && allcount == ids.length) {
                            $(".tipmsg").html($('<span  style="margin-right: 25px">  删除成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                            $(".tipmsg").show();
                            reload();
                        } else {
                            $(".tipmsg").html($('<span  style="margin-right: 25px">  删除失败！请联系管理员</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                            $(".tipmsg").show();
                        }
                        closeTip5sec();
                    }
                });
            }
        };
        $.fn.dataTable.ext.buttons.add = {
            action: function (e, dt, node, config) {
                $("#urlpicker").val("");
                $("#datetimepicker").val("");
                $('.create').click();
            }
        };
        table = $('#dateTable').DataTable({
            dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'batchDel',
                    text: '批量删除'
                }, {
                    extend: 'add',
                    text: '新增'
                }
            ],
            pagingType: "simple_numbers",
            language: {
                sSearch: "搜索:",
                sEmptyTable: "这里空空的，什么也找不到",
                info: "显示从_START_到_END_总共_TOTAL_条数据",
                lengthMenu: "默认 _MENU_ 条数据",
                paginate: {
                    next: "下一页",
                    previous: "上一页"
                },
            },
            "ajax": {
                async: 'false',
                url: "bgworkdate",
                type: 'GET',
                dataSrc: function (myjson) {
                    return myjson;
                }
            },
            columns: [{
                data: "pk",
                className: "dt-body-center",
                render: function (data, type, row, meta) {
                    return `<input type="checkbox" name="cb_select" value="${data},${row.status}">`;
                }
            }, {
                className: "dt-body-center",
                data: "pk",
            }, {
                className: "dt-body-center",
                data: "createTime",
            }, {
                className: "dt-body-center",
                data: "sheduleTime",
            }, {
                className: "dt-body-center",
                data: "dlUrl",
            }, {
                className: "dt-body-center",
                data: "status",
                render: function (data, type, row, meta) {
                    if (data == 0)
                        return "等待执行";
                    else if (data == 1) {
                        return "执行成功";
                    } else {
                        return "执行失败";
                    }
                }
            }, {
                data: "pk",
                className: "dt-body-center",
                render: function (data, type, row, meta) {
                    return `<button type='button' class='btn btn-success btn-sm' onclick='remove("${data}","${row.status}")'>删除</button>`;
                }
            }
            ],
        });

    });

    /*创建了就不能修改了*/
    /*  <button type='button' class='btn btn-success btn-sm' onclick='edit("${row.dlUrl}","${row.sheduleTime}","${data}")'>编辑</button>&nbsp;*/
    function edit(url, sheduletime, id) {
        $('#fileid').html(id);
        $("#urlpicker").val(url);
        $("#datetimepicker").val(sheduletime);
        $('.create').click();
    }

    function remove(id, status) {
        if (status == 0) {
            $(".tipmsg").html($('<span  style="margin-right: 25px"> 任务已经部署待执行，无法删除,待执行完成后删除！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
            $(".tipmsg").show();
            closeTip2sec();
            return;
        }
        $.ajax({
            url: "batchDelBGWork",
            type: 'POST',
            data: {pks: id},
            success: function (result) {
                if (result.status == "success") {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  删除成功！</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                    reload();
                } else {
                    $(".tipmsg").html($('<span  style="margin-right: 25px">  删除失败！请联系管理员</span><i class="fas fa-times" onclick="closeTip()"></i>'));
                    $(".tipmsg").show();
                }
                closeTip2sec();
            }
        });
    }

    function reload() {
        table.ajax.reload();
    }

    function closeTip() {
        $(".tipmsg").html("");
        $(".tipmsg").hide();
    }

    function closeTip2sec() {
        setTimeout(function closeTip() {
            $(".tipmsg").html("");
            $(".tipmsg").hide();
        }, 2000);
    }

</script>
</body>
</html>
